package app;

import static org.testng.Assert.assertEquals;
import java.net.MalformedURLException;
import io.appium.java_client.AppiumBy;
import io.appium.java_client.android.AndroidDriver;
//import io.appium.java_client.TouchAction;
//import io.appium.java_client.touch.LongPressOptions;
//import io.appium.java_client.touch.offset.ElementOption;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.net.URL;
//import java.time.Duration;

public class Mission14 {

	  AndroidDriver driver;

	  @BeforeClass
	  public void InitDriver() throws MalformedURLException {
	    DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
	    URL url = new URL("http://127.0.0.1:4723/wd/hub");
	    desiredCapabilities.setCapability("appium:deviceName", "127.0.0.1:4723");
	    desiredCapabilities.setCapability("platformName", "Android");
		desiredCapabilities.setCapability("appiumPackage","com.example.shixun_11_22");
	    desiredCapabilities.setCapability("appium:ensureWebviewsHavePages", true);
	    desiredCapabilities.setCapability("appium:nativeWebScreenshot", true);
	    desiredCapabilities.setCapability("appium:newCommandTimeout", 3600);
	    desiredCapabilities.setCapability("appium:connectHardwareKeyboard", true);

	    driver = new AndroidDriver(url, desiredCapabilities);
	  }

	  @Test
//		测试注册
	    public void testEnroll() throws InterruptedException {
//		  点击“我的”，点击注册，分别输入账号密码
	        driver.findElement(AppiumBy.xpath("//*[@text='我的']")).click();
	        Thread.sleep(1000);
	        driver.findElement(AppiumBy.id("com.example.shixun_11_22:id/user_register")).click();
	        Thread.sleep(1000);
	        driver.findElement(AppiumBy.id("com.example.shixun_11_22:id/username_et"))
	        .sendKeys("sasara");
	        Thread.sleep(1000);
	        driver.findElement(AppiumBy.id("com.example.shixun_11_22:id/password_et"))
	        .sendKeys("123456");
	        Thread.sleep(1000);
	        driver.findElement(AppiumBy.id("com.example.shixun_11_22:id/register_btn")).click();
	        Thread.sleep(1000);
	      
	        }
	  
//		测试登录
	    @Test
	    public void testLogin() throws InterruptedException {
	        driver.findElement(AppiumBy.xpath("//*[@text='我的']")).click();
	        Thread.sleep(1000);
	        driver.findElement(AppiumBy.id("com.example.shixun_11_22:id/username_et"))
	        .sendKeys("sasara");
	        Thread.sleep(1000);
	        driver.findElement(AppiumBy.id("com.example.shixun_11_22:id/password_et"))
	        .sendKeys("123456");
	        Thread.sleep(1000);
	        driver.findElement(AppiumBy.id("com.example.shixun_11_22:id/user_login")).click();
	        Thread.sleep(1000);
	        }
	    
//	    修改信息(非法昵称)
	    @Test
	    private void modifyinfor() throws InterruptedException {
	    	driver.findElement(AppiumBy.id("com.example.shixun_11_22:id/btn_modify_info")).click();
	    	Thread.sleep(1000);
	    	driver.findElement(AppiumBy.id("com.example.shixun_11_22:id/nick_edt"))
	    	.sendKeys("~^&*()_     Moon in Sunshine   !@#$%+");
	    	Thread.sleep(1000);
	    	driver.findElement(AppiumBy.id("com.example.shixun_11_22:id/submit_btn")).click();
	    	Thread.sleep(1000);
		}
	    

//	    关卡1.4
	    @Test
	    private void game4() throws InterruptedException {
//	    	闯关模式
	    	driver.findElement(AppiumBy.id("com.example.shixun_11_22:id/mode")).click();
	    	Thread.sleep(1000);
	    	driver.findElement(AppiumBy.linkText("关卡  1.4")).click();
	    	Thread.sleep(1000);
//	    	点击编码，选择计算中的循环选择次数10，再点击顺时针旋转嵌套在循环中，并设置值为36，点击确定
//	    	编码
	    	driver.findElement(AppiumBy.id("com.example.shixun_11_22:id/display_btn_code")).click();
	    	Thread.sleep(1000);
//	    	点击计算
	    	driver.findElement(AppiumBy.id("com.example.shixun_11_22:id/animation_model_tv")).click();
	    	Thread.sleep(1000);
//	    	选择循环
	    	driver.findElement(AppiumBy.linkText("循  环")).click();
	    	Thread.sleep(1000);
//	    	开始
	    	driver.findElement(AppiumBy.xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.view.ViewGroup[1]/android.widget.ImageView")).click();
	    	Thread.sleep(1000);
//	    	长按循环
	    	WebElement element1 = driver.findElement(AppiumBy.xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.view.ViewGroup[3]/android.widget.ImageView"));
	    	Actions action1 = new Actions(driver);
	    	action1.clickAndHold(element1).perform();
	    	Thread.sleep(1000);
//	    	输入循环次数10
	    	driver.findElement(AppiumBy.xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.LinearLayout")).sendKeys("10");
	    	Thread.sleep(1000);
//	    	点击动画
	    	driver.findElement(AppiumBy.id("com.example.shixun_11_22:id/animation_model_tv")).click();
	    	Thread.sleep(1000);
//	    	点击顺时针旋转
	    	driver.findElement(AppiumBy.linkText("顺时针旋转")).click();
	    	Thread.sleep(1000);
//	    	点击循环
	    	driver.findElement(AppiumBy.xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.view.ViewGroup[3]/android.widget.ImageView")).click();
	    	Thread.sleep(1000);
//	    	长按顺时针旋转
	    	WebElement element = driver.findElement(AppiumBy.xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.view.ViewGroup[3]/android.widget.ImageView"));
	    	Actions action = new Actions(driver);
	    	action.clickAndHold(element).perform();
//	    	WebElement element = driver.findElement(AppiumBy.linkText("顺时针旋转"));
//	    	TouchAction action = new TouchAction(driver);
//          action.longPress(LongPressOptions.longPressOptions().withElement(ElementOption.element(element)).withDuration(Duration.ofSeconds(2))).release().perform();
//			输入变量名
	    	driver.findElement(AppiumBy.id("com.example.shixun_11_22:id/edt_modify_anima")).sendKeys("36");
	    	Thread.sleep(1000);
//			点击确认输入变量名
	    	driver.findElement(AppiumBy.id("com.example.shixun_11_22:id/btn_modify_anima")).click();
	    	Thread.sleep(1000);
//	    	点击确认
	    	driver.findElement(AppiumBy.id("com.example.shixun_11_22:id/confirm_bt")).click();
//	    	验证是否通关
	    	WebElement conclusion = driver.findElement(AppiumBy.id("android:id/message"));
			System.out.println(conclusion.getText());
			assertEquals("恭喜你，通关成功！", conclusion.getText());

	    }

//	    关卡1.5
	    @Test
	    private void game5() throws InterruptedException {
//	    	闯关模式
	    	driver.findElement(AppiumBy.id("com.example.shixun_11_22:id/mode")).click();
	    	Thread.sleep(1000);
	    	driver.findElement(AppiumBy.linkText("关卡  2.1")).click();
	    	Thread.sleep(1000);
//	    	点击编码，设置变量x=1
//	    	点击编码
	    	driver.findElement(AppiumBy.id("com.example.shixun_11_22:id/display_btn_code")).click();
	    	Thread.sleep(1000);
//	    	点击计算
	    	driver.findElement(AppiumBy.id("com.example.shixun_11_22:id/animation_model_tv")).click();
	    	Thread.sleep(1000);
//	    	点击变量
	    	driver.findElement(AppiumBy.xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.ListView/android.widget.LinearLayout[7]/android.widget.Button")).click();
	    	Thread.sleep(1000);
//	    	开始
	    	driver.findElement(AppiumBy.xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.view.ViewGroup[1]/android.widget.ImageView")).click();
	    	Thread.sleep(1000);
//	    	长按变量赋值
	    	WebElement element = driver.findElement(AppiumBy.xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.view.ViewGroup[2]/android.widget.ImageView"));
	    	Actions action = new Actions(driver);
	    	action.clickAndHold(element).perform();
//	    	输入变量名
	    	driver.findElement(AppiumBy.id("com.example.shixun_11_22:id/variable_name_edt")).sendKeys("x");
	    	Thread.sleep(1000);
//	    	输入变量值
	    	driver.findElement(AppiumBy.id("com.example.shixun_11_22:id/variable_value_edt")).sendKeys("1");
	    	Thread.sleep(1000);
//	    	点击确定
	    	driver.findElement(AppiumBy.id("com.example.shixun_11_22:id/variable_enter_btn")).click();
	    	Thread.sleep(1000);
//	    	选择计算中的循环选择次数3
//	    	点击计算
	    	driver.findElement(AppiumBy.id("com.example.shixun_11_22:id/animation_model_tv")).click();
	    	Thread.sleep(1000);
//	    	选择循环
	    	driver.findElement(AppiumBy.linkText("循  环")).click();
	    	Thread.sleep(1000);
//	    	开始
	    	driver.findElement(AppiumBy.xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.view.ViewGroup[1]/android.widget.ImageView")).click();
	    	Thread.sleep(1000);
//	    	长按循环
	    	WebElement element1 = driver.findElement(AppiumBy.xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.view.ViewGroup[3]/android.widget.ImageView"));
	    	Actions action1 = new Actions(driver);
	    	action1.clickAndHold(element1).perform();
	    	Thread.sleep(1000);
//	    	再点击输出变量X（不换行）并点击加法X=X+1嵌套在循环中，点击确定
//	    	点击输出
	    	driver.findElement(AppiumBy.id("com.example.shixun_11_22:id/output_model_tv")).click();
	    	Thread.sleep(1000);
//	    	点击变量
	    	driver.findElement(AppiumBy.xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.ListView/android.widget.LinearLayout[1]/android.widget.Button")).click();
	    	Thread.sleep(1000);
//	    	点击循环
	    	driver.findElement(AppiumBy.xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.view.ViewGroup[3]/android.widget.ImageView")).click();
	    	Thread.sleep(1000);
//	    	长按变量
	    	WebElement element2 = driver.findElement(AppiumBy.xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.view.ViewGroup[4]/android.widget.ImageView"));
	    	Actions action2 = new Actions(driver);
	    	action2.clickAndHold(element2).perform();
	    	Thread.sleep(1000);
//	    	滚动选择变量num
	    	driver.findElement(AppiumBy.id("com.example.shixun_11_22:id/variable_enter_btn")).sendKeys("x");
	    	Thread.sleep(1000);
//	    	点击不换行
	    	driver.findElement(AppiumBy.id("com.example.shixun_11_22:id/output_model_tv")).sendKeys("不换行");
	    	Thread.sleep(1000);
//	    	点击修改
	    	driver.findElement(AppiumBy.id("com.example.shixun_11_22:id/btn_modify_outputV")).click();
	    	Thread.sleep(1000);
//	    	点击计算
	    	driver.findElement(AppiumBy.id("com.example.shixun_11_22:id/animation_model_tv")).click();
	    	Thread.sleep(1000);
//	    	点击加法
	    	driver.findElement(AppiumBy.linkText("加")).click();
	    	Thread.sleep(1000);
//	    	点击循环
	    	driver.findElement(AppiumBy.xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.view.ViewGroup[3]/android.widget.ImageView")).click();
	    	Thread.sleep(1000);
//	    	长按加
	    	WebElement element3 = driver.findElement(AppiumBy.xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.view.ViewGroup[5]/android.widget.ImageView"));
	    	Actions action3 = new Actions(driver);
	    	action3.clickAndHold(element3).perform();
	    	Thread.sleep(1000);
//	    	选择变量名
	    	driver.findElement(AppiumBy.id("com.example.shixun_11_22:id/picker_var_list")).sendKeys("x");
	    	Thread.sleep(1000);
//	    	输入值1
	    	driver.findElement(AppiumBy.id("com.example.shixun_11_22:id/edt_one")).sendKeys("x");
	    	Thread.sleep(1000);
//	    	输入值2
	    	driver.findElement(AppiumBy.id("com.example.shixun_11_22:id/edt_three")).sendKeys("1");
	    	Thread.sleep(1000);
//	    	点击修改
	    	driver.findElement(AppiumBy.id("com.example.shixun_11_22:id/btn_modify_calc")).click();
//	    	点击确认
	    	driver.findElement(AppiumBy.id("com.example.shixun_11_22:id/confirm_bt")).click();
//	    	验证是否通关
	    	WebElement conclusion = driver.findElement(AppiumBy.id("android:id/message"));
			System.out.println(conclusion.getText());
			assertEquals("恭喜你，通关成功！", conclusion.getText());
	    	  }
}


